﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Contoso.Models;

namespace Contoso
{
    public class SchoolContext:DbContext
    {

        public DbSet<Course> Courses { set; get; }

        public DbSet<Student> Students { set; get; }

        public DbSet<Enrollment> Enrollments { set; get; }

        public DbSet<Instructor> Instructors { get; set; }

        public DbSet<Department> Departments { set; get; }

        public DbSet<OfficeAssignment> OfficeAssignments { get; set; } 


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           // base.OnModelCreating(modelBuilder);


            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Instructor>().HasOptional(p => p.OfficeAssignment).WithRequired(p => p.Instructor);

            modelBuilder.Entity<Course>().HasMany(p => p.Instructors).WithMany(i => i.Courses)
                .Map(t => t.MapLeftKey("CourseID")
                    .MapRightKey("InstructorID")
                    .ToTable("CourseInstructor"));

            modelBuilder.Entity<Department>().HasOptional(x=>x.Administrator);
        }
    }

    


}